package me.zhenqu.services.factory;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-10
 * Time: 下午1:25
 * To change this template use File | Settings | File Templates.
 */
public final class ByteStreamMaker {
    public static byte[] serilize(Object object){
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        try{
            ObjectOutputStream oStream = new ObjectOutputStream( bStream );
            oStream.writeObject ( object );
            return bStream.toByteArray();
        }catch (IOException e){
            return null;
        }
    }

    public static Object deserilize(byte[] bytes){
        try{
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            ObjectInputStream is = new ObjectInputStream(in);
            try {
                return is.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

}
